using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace MinoChat.Converters;

public class BoolInverterConverter : IValueConverter
{
    //Convert:vm=>view。当IsBusy为true时，返回Visibility.Collapsed
    //<Button Content="Click me" Visibility="{Binding IsBusy, Converter={StaticResource BoolInverterConverter}}"/>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool boolValue)
        {
            // 如果目标类型是Visibility，返回Visibility枚举
            if (targetType == typeof(Visibility) || targetType == typeof(Visibility?))
            {
                return !boolValue ? Visibility.Visible : Visibility.Collapsed;
            }
            // 否则返回反转的布尔值
            return !boolValue;
        }

        return targetType == typeof(Visibility) || targetType == typeof(Visibility?)
            ? Visibility.Visible
            : true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool boolValue)
        {
            return !boolValue;
        }

        return false;
    }
}